package com.itheima.web.old;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author Tensin
 * @version 1.0
 * @date
 */
//@WebServlet("/addServlet")
public class AddServlet extends HttpServlet {

    private BrandService brandService = new BrandServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //先接收从前端页面传来的Json数据
        BufferedReader reader = request.getReader();
        String jsonParam = reader.readLine();//Json的字符串

        //转成Brand对象
        Brand brand = JSON.parseObject(jsonParam, Brand.class);

        //调用Service添加寻
        brandService.add(brand);

        //响应成功标识
        response.getWriter().write("success");
    }
}
